#include "myserver.h"
#include <QDebug>
#include <formsrv.h>
MyServer::MyServer(QObject *parent) :
    QTcpServer(parent)
{
    pool  = QThreadPool::globalInstance();
    pool->setMaxThreadCount(100);
    m_bIsStart = false;
    m_handle = 0;
    m_currentTask = NULL;
    m_socDes2TaskMap.clear();
    m_Ip2TaskMap.clear();
    m_qClientIp.clear();
    m_Ip2RowNumMap.clear();
}

MyServer::~MyServer()
{
    //qDebug()<<"close server";
}
MyServer* getServer()
{
    static MyServer* srv = NULL;
    if(srv == NULL)
    {
        srv = new MyServer();
    }
    return srv;
}
bool MyServer::StartServer(quint16 port)
{

    if(m_bIsStart)
    {
        return true;
    }
    if(this->isListening())
    {
        //qDebug()<< "Server has Started";
        return true;
    }
    if(this->listen(QHostAddress::Any,port))
    {
        m_bIsStart = true;
        emit start();
        //qDebug()<< "Server Started";
        return true;
    }
    else
    {
        //qDebug()<< "Server did not Started"<<port;
        return false;
    }
}

void MyServer::StopServer()
{
    if(!m_bIsStart)
    {
        return;
    }
    emit stop();
    this->close();
    pool->waitForDone();
    m_bIsStart = false;
    //qDebug()<<"Server Stoped active thread task"<<pool->activeThreadCount();
}
void MyServer::slot_ThreadCount()
{
    qDebug()<<"current thread count"<<pool->activeThreadCount();
}
void MyServer::incomingConnection(int handle)
{
    //MyClient *client = new MyClient();
    //client->SetSocket(handle);
    //使用线程池
    if(!m_bIsStart)
    {
        return;
    }
    qDebug()<<"create new task";
    MyTask *task = new MyTask();
    task->SocketDescriptor = handle;
    task->setAutoDelete(true);
    connect(task,SIGNAL(sig_RecMsg(int,QString)),getForm(),SLOT(slot_UpdateClientMsg(int,QString)));
    connect(task,SIGNAL(sig_LogMsg(int,QString)),getForm(),SLOT(slot_DisplayLogMsg(int,QString)));
    connect(task,SIGNAL(sig_UpdateClientNetState(int,QString)),getForm(),SLOT(slot_UpdateClientNetState(int,QString)));
    connect(task,SIGNAL(sig_UpdateClientID(int,int)),getForm(),SLOT(slot_UpdateClientID(int,int)));
    connect(task,SIGNAL(sig_UpdateClientInitState(int,QString,QString,QString,QString,QString)),getForm(),SLOT(slot_UpdateClientInitState(int,QString,QString,QString,QString,QString)));
    connect(task,SIGNAL(sig_UpdateVehQueue(int,myQueue,int)),getForm(),SLOT(slot_UpdateVehQueue(int,myQueue,int)));
    connect(task,SIGNAL(sig_UpdateCurVeh(int,myVehInfo)),getForm(),SLOT(slot_UpdateCurVeh(int,myVehInfo)));
    connect(getForm(),SIGNAL(sig_SendMsg(int,QString)),task,SLOT(SendMsg(int,QString)));
    connect(this,SIGNAL(stop()),task,SLOT(Stop()));
    connect(this,SIGNAL(start()),task,SLOT(Start()));
    pool->start(task);
    qDebug()<<"active thread task"<<pool->activeThreadCount();
    //使用多线程
    //    MyThread* thread = new MyThread(handle,this);
    //    connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));
    //    thread->start();
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(slot_ThreadCount()));
    //timer->start(30000);
}
void MyServer::slot_SendMsg(int socket,QString msg)
{
    if(m_socDes2TaskMap.contains(socket))
    {
        m_currentTask = m_socDes2TaskMap.value(socket);
        m_currentTask->SendMsg(socket,msg);
    }
    //pool->moveToThread();
}

//void MyServer::slot_InsetNewIpAndRow(const QString &ip, int rowNum)
//{
//    QMutexLocker lock(&mutex);
//    m_Ip2RowNumMap.insert(ip,rowNum);
//    qDebug()<<tr("插入IP：%1至map").arg(ip);
//}

//bool MyServer::CheckexistIp(const QString &ip, int &rowNum)
//{
//    QMutexLocker lock(&mutex);
//    if(!m_Ip2RowNumMap.contains(ip))
//    {
//        qDebug()<<tr("mpa 中不存在该ip：%1").arg(ip);
//        return false;
//    }
//    else
//    {
//        rowNum = m_Ip2RowNumMap.value(ip);
//        qDebug()<<tr("mpa 中已存在该ip：%1行号：%2").arg(ip).arg(rowNum);
//        return true;
//    }
//}

